/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.equipment.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Result;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.equipment.entity.EleMonitorParam;
import com.thinkgem.jeesite.modules.equipment.service.EleMonitorParamService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 设备监控Controller
 * 
 * @author unnamed
 * @version 2018-01-10
 */
@Controller
@RequestMapping(value = "${adminPath}/equipment/eleMonitorParam")
public class EleMonitorParamController extends BaseController {

	@Autowired
	private EleMonitorParamService eleMonitorParamService;

	@ModelAttribute
	public EleMonitorParam get(@RequestParam(required = false) String id) {
		EleMonitorParam entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = eleMonitorParamService.get(id);
		}
		if (entity == null) {
			entity = new EleMonitorParam();
		}
		return entity;
	}

	@RequiresPermissions("equipment:eleMonitorParam:view")
	@RequestMapping(value = { "list", "" })
	public String list(EleMonitorParam eleMonitorParam, HttpServletRequest request, HttpServletResponse response,
			Model model, String equipmentId) {
		EleMonitorParam cond = new EleMonitorParam();
		cond.setEquipmentId(equipmentId);
		List<EleMonitorParam> params = eleMonitorParamService.findList(cond);
		model.addAttribute("paramsList", params);
		return "modules/equipment/eleMonitorParamList";
	}

	@RequiresPermissions("equipment:eleMonitorParam:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public Result save(String paramList, HttpServletRequest request) {
		User current = UserUtils.getUser();
		logger.info(paramList);
		paramList = paramList.replace("&quot;", "\"");
		JSONArray json = JSONArray.parseArray(paramList);
		logger.info(json.toJSONString());
		for (int i = 0; i < json.size(); i++) {
			JSONObject param = JSONObject.parseObject(json.get(i).toString());
			logger.info(param.toJSONString());
			String paramId = param.getString("id");
			String paramName = param.getString("name");
			String paramReveiver = param.getString("reveiver");
			String paramValue = param.getString("value");
			EleMonitorParam cond = new EleMonitorParam();
			cond.setId(paramId);
			cond.setName(paramName);
			cond.setThreshold(paramValue);
			cond.setReceiverType(paramReveiver);
			cond.setUpdateBy(current);
			cond.setUpdateDate(new Date(System.currentTimeMillis()));
			cond.setIsUsed("0");
			eleMonitorParamService.update(cond);
		}
		String msg = "更新成功";
		return new Result(Result.SUCCESS, msg, null);

	}

	@RequiresPermissions("equipment:eleMonitorParam:edit")
	@RequestMapping(value = "delete")
	public String delete(EleMonitorParam eleMonitorParam, RedirectAttributes redirectAttributes) {
		eleMonitorParamService.delete(eleMonitorParam);
		addMessage(redirectAttributes, "删除设备监控成功");
		return "redirect:" + Global.getAdminPath() + "/equipment/eleMonitorParam/?repage";
	}

}